<template>
  <div style="display:flex;">
    <el-card style="width: 400px;">
      <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="用户昵称" prop="name">
          <el-input type="text" v-model="ruleForm.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="用户账户" prop="username">
          <el-input type="text" v-model="ruleForm.username" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="用户密码" prop="password">
          <el-input type="password" v-model="ruleForm.password" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="checkPass">
          <el-input type="password" v-model="ruleForm.checkPass" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="住宅电话" prop="telephone">
          <el-input type="text" v-model="ruleForm.telephone" autocomplete="off" placeholder="例如：029-82881234"></el-input>
        </el-form-item>
        <el-form-item label="手机号码" prop="phone">
          <el-input type="text" v-model="ruleForm.phone" autocomplete="off" placeholder="例如：13075482685"></el-input>
        </el-form-item>
        <el-form-item label="居住地址" prop="address">
          <el-input type="text" v-model="ruleForm.address" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button size="mini" type="primary" @click="submitForm('ruleForm')">添加</el-button>
          <el-button size="mini" @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    var validateUsername = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入用户名'));
      } else {

        callback();
      }
    };
    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'));
      } else {
        if (this.ruleForm.checkPass !== '') {
          this.$refs.ruleForm.validateField('checkPass');
        }
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'));
      } else if (value !== this.ruleForm.password) {
        callback(new Error('两次输入密码不一致!'));
      } else {
        callback();
      }
    };
    var validatePhone = (rule, value, callback) => {
      // const isPhone = /^1([38][0-9]|4[014-9]|[59][0-35-9]|6[2567]|7[0-8])\d{8}$/ //11位合法手机号码
      const isMob1 = /0\d{2,3}\d{7,8}/ //不带-的座机：02985111752
      const isMob2 = /^([0-9]{3,4}-)?[0-9]{7,8}$/ //带一个-：029-85111752
      const isMob3 = /^\d{3,4}-\d{3,4}-\d{3,4}$/; //带三个-： 0752-250-520
      if (isMob1.test(value) || isMob2.test(value) || isMob3.test(value)) {
        callback();
      } else {
        callback(new Error('电话号码格式错误！'));
      }
    };
    var validateName = (rule, value, callback) => {
      let specialKey = "[`~!#$^&*()=|{}':;'\\[\\].<>/?~！#￥……&*（）——|{}【】‘；：”“'。，、？]‘'";
      for (let i = 0; i < value.length; i++) {
        if (specialKey.indexOf(value.substr(i, 1)) !== -1) {
          callback(new Error('不能含有特殊字符！'));
        }
      }
      callback();
    };
    return {
      ruleForm: {
        name: '',
        username: '',
        password: '',
        checkPass: '',
        telephone: '',
        phone: '',
        address: ''
      },
      rules: {
        name: [
          { required: true, message: '请输入用户昵称', trigger: 'blur' },
          { min: 2, max: 8, message: '用户昵称长度为2-8位', trigger: 'blur' },
          {validator: validateName, trigger: 'blur'}
        ],
        username: [
          { required: true, message: '请输入用户账户', trigger: 'blur' },
          { min: 4, max: 8, message: '用户账户长度为4-8位', trigger: 'blur' },
          {validator: validateUsername, trigger: 'blur'}
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 3, max: 8, message: '密码长度为3-8位', trigger: 'blur' },
          {validator: validatePass, trigger: 'blur'}
        ],
        checkPass: [
          { required: true, message: '请再次输入密码', trigger: 'blur' },
          { min: 3, max: 8, message: '密码长度为3-8位', trigger: 'blur' },
          {validator: validatePass2, trigger: 'blur'}
        ],
        telephone: [
          { required: false, message: '请输入住宅电话', trigger: 'blur' },
          {validator: validatePhone, trigger: 'blur'}
        ],
        phone: [
          { required: true, message: '请输入手机号码', trigger: 'blur' },
          { type: 'number',
            message: '手机号格式不正确',
            trigger: 'blur',
            transform(value) {
              var phonereg = 11 && /^((13|14|15|16|17|18|19)[0-9]{1}\d{8})$/
              if (!phonereg.test(value)) {
                return false
              } else {
                return Number(value)
              }
            }
          }
        ],
        address: [
          { required: true, message: '请输入居住地址', trigger: 'blur' },
          {validator: validateName, trigger: 'blur'}
        ]
      }
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.postRequest("/system/hr/reg", this.ruleForm).then(resp => {
            if (resp && resp.status == 200) {
              console.log('success')
            }
          });
        } else {
          console.log('error submit!!');
          return false;
        }
      });
      this.resetForm();
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
}
</script>

<style scoped>

</style>
